package com.foreknow.tijian.dao.Impl;

import com.foreknow.tijian.dao.HospitalDao;
import com.foreknow.tijian.po.Hospital;
import com.foreknow.tijian.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class HospitalDaoImpl implements HospitalDao {
    private Connection con;
    private PreparedStatement pre;
    private ResultSet rs;
    @Override
    public List<Hospital> listHospital(Hospital hospital) throws SQLException {
        List<Hospital> hospitals = new ArrayList<Hospital>();
        int state = hospital.getState();
        String sql = "select * from hospital where state=? ";
        try {
            //1.连接数据库
            con = DBUtil.getConnection();
            //2.获取到预处理对象
            pre = con.prepareStatement(sql);
            //把问号替换为具体的值
            pre.setInt(1, state);
            //3.执行查询操作
            rs = pre.executeQuery();
            while (rs.next()) {
               Hospital hospital1 = new Hospital();
               hospital1.setHpId(rs.getInt("hpId"));
               hospital1.setName(rs.getString("name"));
                hospital1.setPicture(rs.getString("picture"));
                hospital1.setTelephone(rs.getString("telephone"));
                hospital1.setState(rs.getInt("state"));
               hospital1.setBusinessHours(rs.getString("businessHours"));
               hospital1.setDeadline(rs.getString("deadline"));
               hospital1.setAddress(rs.getString("address"));
               hospital1.setRule(rs.getString("rule"));
               hospital1.setState(rs.getInt("state"));
                hospitals.add(hospital1);
            }
        } finally {
            DBUtil.close(pre);
        }
        return hospitals;
    }

    @Override
    public Hospital getHospitalById(Hospital hospital) throws SQLException {
        int hpId = hospital.getHpId();
        String sql = "select * from hospital where hpId=?";
        Hospital hospital1 = new Hospital();
        try{
            //1.连接数据库
            con = DBUtil.getConnection();
            //2.获取到预处理对象
            pre = con.prepareStatement(sql);
            //把问号替换为具体的值
            pre.setInt(1, hpId);
            //3.执行查询操作
            rs = pre.executeQuery();
            if(rs.next()){
                hospital1.setHpId(rs.getInt("hpId"));
                hospital1.setName(rs.getString("name"));
                hospital1.setPicture(rs.getString("picture"));
                hospital1.setTelephone(rs.getString("telephone"));
                hospital1.setBusinessHours(rs.getString("businessHours"));
                hospital1.setDeadline(rs.getString("deadline"));
                hospital1.setAddress(rs.getString("address"));
                hospital1.setRule(rs.getString("rule"));
                hospital1.setState(rs.getInt("state"));
            }
        }finally {
            DBUtil.close(pre);
        }
        return hospital1;
    }
}
